/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.basic.hazard.source.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.hazard.source.dto.HazMonitorFactorDTO;
import com.hopes.bizz.basic.hazard.source.dto.HazMonitorFactorExcelDTO;
import com.hopes.bizz.basic.hazard.source.entity.HazHazardSourceEntity;
import com.hopes.bizz.basic.hazard.source.entity.HazMonitorFactorEntity;
import com.hopes.bizz.basic.hazard.source.service.HazHazardSourceService;
import com.hopes.bizz.basic.hazard.source.service.HazMonitorFactorService;
import com.hopes.bizz.basic.hazard.source.vo.HazMonitorFactorExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 危险源监控因子设置
 *
 * @author daijie
 * @date 2023-11-01 17:32:11
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/monitorFactor")
@Tag(description = "monitorFactor", name = "危险源监控因子设置管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazMonitorFactorController {

    private final HazMonitorFactorService hazMonitorFactorService;
    private final HazHazardSourceService hazHazardSourceService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param hazMonitorFactor 危险源监控因子设置
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_view')")
    public R gethazMonitorFactorPage(Page page, HazMonitorFactorDTO hazMonitorFactor) {
        LambdaQueryWrapper<HazMonitorFactorEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getHazardSourceId()), HazMonitorFactorEntity::getHazardSourceId, hazMonitorFactor.getHazardSourceId());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getFactorId()), HazMonitorFactorEntity::getFactorId, hazMonitorFactor.getFactorId());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getUseStatus()), HazMonitorFactorEntity::getUseStatus, hazMonitorFactor.getUseStatus());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getOrgId()), HazMonitorFactorEntity::getOrgId, hazMonitorFactor.getOrgId());
        wrapper.like(StringUtils.isNotBlank(hazMonitorFactor.getFactorName()), HazMonitorFactorEntity::getFactorName, hazMonitorFactor.getFactorName());


        wrapper.orderByAsc(HazMonitorFactorEntity::getHazardSourceId, HazMonitorFactorEntity::getSortNum);

        if (StringUtils.isNotBlank(hazMonitorFactor.getHazardSourceLevel()) || StringUtils.isNotBlank(hazMonitorFactor.getHazardName())) {
            LambdaQueryWrapper<HazHazardSourceEntity> sourceWrapper = Wrappers.lambdaQuery();
            sourceWrapper.eq(StringUtils.isNotBlank(hazMonitorFactor.getHazardSourceLevel()), HazHazardSourceEntity::getHazardSourceLevel, hazMonitorFactor.getHazardSourceLevel());
            sourceWrapper.like(StringUtils.isNotBlank(hazMonitorFactor.getHazardName()), HazHazardSourceEntity::getHazardName, hazMonitorFactor.getHazardName());
            List<Object> ids = hazHazardSourceService.getBaseMapper().selectObjs(sourceWrapper);
            if (ids.isEmpty()) {
                return R.ok(Collections.emptyList());
            }
            wrapper.in(HazMonitorFactorEntity::getHazardSourceId, ids);
        }
        return R.ok(hazMonitorFactorService.page(page, wrapper));
    }

    /**
     * 查询列表
     *
     * @return 查询列表
     */
    @Operation(summary = "查询列表", description = "查询列表")
    @GetMapping("/list")
    @Inner(value = false)
    public R getHazMonitorFactorList() {
        return R.ok(hazMonitorFactorService.list(Wrappers.lambdaQuery(HazMonitorFactorEntity.class)
                .eq(HazMonitorFactorEntity::getUseStatus, true)));
    }

    /**
     * 获取个数
     *
     * @return 获取个数
     */
    @Operation(summary = "获取个数", description = "获取个数")
    @PostMapping("/count")
    @Inner(value = false)
    public R getHazHazardSourceCount(HazMonitorFactorEntity monitorFactor) {
        LambdaQueryWrapper<HazMonitorFactorEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotNull(monitorFactor.getHazardSourceId()), HazMonitorFactorEntity::getHazardSourceId, monitorFactor.getHazardSourceId());
        return R.ok(hazMonitorFactorService.count(wrapper));
    }

    /**
     * 通过id查询危险源监控因子设置
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_view')")
    @Inner(value = false)
    public R getById(@PathVariable("id") Long id) {
        return R.ok(hazMonitorFactorService.getById(id));
    }

    /**
     * 监控因子树
     *
     * @param hazMonitorFactor 危险源监控因子设置
     * @return
     */
    @Operation(summary = "监控因子树", description = "监控因子树")
    @GetMapping("/tree")
//    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_view')")
    public R monitorFactorTree(HazMonitorFactorDTO hazMonitorFactor) {
        return R.ok(hazMonitorFactorService.monitorFactorTree(hazMonitorFactor));
    }

    /**
     * 新增危险源监控因子设置
     *
     * @param hazMonitorFactor 危险源监控因子设置
     * @return R
     */
    @Operation(summary = "新增危险源监控因子设置", description = "新增危险源监控因子设置")
    @SysLog("新增危险源监控因子设置")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_add')")
    public R save(@RequestBody HazMonitorFactorEntity hazMonitorFactor) {
        return R.ok(hazMonitorFactorService.save(hazMonitorFactor));
    }

    /**
     * 修改危险源监控因子设置
     *
     * @param hazMonitorFactor 危险源监控因子设置
     * @return R
     */
    @Operation(summary = "修改危险源监控因子设置", description = "修改危险源监控因子设置")
    @SysLog("修改危险源监控因子设置")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_edit')")
    @Inner(value = false)
    public R updateById(@RequestBody HazMonitorFactorEntity hazMonitorFactor) {
        return R.ok(hazMonitorFactorService.updateById(hazMonitorFactor));
    }

    /**
     * 通过id删除危险源监控因子设置
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除危险源监控因子设置", description = "通过id删除危险源监控因子设置")
    @SysLog("通过id删除危险源监控因子设置")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazMonitorFactorService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_export')")
    public List<HazMonitorFactorExcelVO> template() {
        return hazMonitorFactorService.exportExcel(Collections.singletonList(new HazMonitorFactorEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param hazMonitorFactor 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_export')")
    public List<HazMonitorFactorExcelVO> export(HazMonitorFactorDTO hazMonitorFactor) {
        LambdaQueryWrapper<HazMonitorFactorEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getHazardSourceId()), HazMonitorFactorEntity::getHazardSourceId, hazMonitorFactor.getHazardSourceId());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getFactorId()), HazMonitorFactorEntity::getFactorId, hazMonitorFactor.getFactorId());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getUseStatus()), HazMonitorFactorEntity::getUseStatus, hazMonitorFactor.getUseStatus());
        wrapper.eq(ObjectUtils.isNotNull(hazMonitorFactor.getOrgId()), HazMonitorFactorEntity::getOrgId, hazMonitorFactor.getOrgId());
        wrapper.like(StringUtils.isNotBlank(hazMonitorFactor.getFactorName()), HazMonitorFactorEntity::getFactorName, hazMonitorFactor.getFactorName());
        return hazMonitorFactorService.exportExcel(hazMonitorFactorService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param hazMonitorFactorExcelDtoList 查询条件
     * @param bindingResult                错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('haz_monitorFactor_export')")
    public R importExcel(@RequestExcel List<HazMonitorFactorExcelDTO> hazMonitorFactorExcelDtoList, BindingResult bindingResult) {
        return hazMonitorFactorService.importExcel(hazMonitorFactorExcelDtoList, bindingResult);
    }
}
